import 'package:flutter/material.dart';
import 'package:flutter_application_cyber/pages/Mian/cn_login_page.dart';
import '../Home/cn_home_page.dart';
import '../Message/cn_message_page.dart';
import '../Mine/cn_mine_page.dart';
import '../Mate/cn_mate_home_page.dart';
import 'package:flutter_application_cyber/tools/cn_event_bus.dart';

///应用主页
class CnTabaBarPage extends StatefulWidget {
  const CnTabaBarPage({Key? key}) : super(key: key);

  @override
  State<CnTabaBarPage> createState() => _CnTabaBarPageState();
}

class _CnTabaBarPageState extends State<CnTabaBarPage>
    with AutomaticKeepAliveClientMixin {
  final List<Widget> _pages = [
    const CnMateCnHomePage(),
    const CnHomePage(),
    const CnMessagePage(),
    const CnMinePage(),
  ];
  int _index = 0;

  int _badgeCount = 0;

  int _currentCoins = -1;

  @override
  void initState() {
    super.initState();

    //接收到Token失效消息，退出到登录页面
    CnEventBus.instance?.addListener(EventKeys.onTokenExpireCode, (arg) {
      // print("消息接收：退出页面");
      Navigator.of(context).pushAndRemoveUntil(
        MaterialPageRoute(
          builder: (context) => const CnLoginPage(),
        ),
        (route) => false, // 这里返回false以删除所有现有的路由
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        child: Scaffold(
          body: Center(
            child: Stack(
              children: [
                IndexedStack(
                  index: _index,
                  children: _pages,
                ),
              ],
            ),
          ),
          bottomNavigationBar: BottomNavigationBar(
            currentIndex: _index,
            onTap: (value) {
              setState(() {
                _index = value;
              });
            },
            selectedItemColor: const Color(0xff333333),
            unselectedItemColor: const Color(0xffADADAD),
            items: [
              BottomNavigationBarItem(
                icon: Image.asset(
                    width: 28.5, height: 28.5, "images/ic_mate_false.png"),
                activeIcon: Image.asset(
                    width: 28.5, height: 28.5, "images/ic_mate_true.png"),
                label: '',
              ),
              BottomNavigationBarItem(
                icon: Image.asset(
                    width: 28.5,
                    height: 28.5,
                    "images/ic_anchor_wall_false.png"),
                activeIcon: Image.asset(
                    width: 28.5,
                    height: 28.5,
                    "images/ic_anchor_wall_true.png"),
                label: '',
              ),
              BottomNavigationBarItem(
                icon: Image.asset(
                    width: 28.5, height: 28.5, "images/ic_message_false.png"),
                activeIcon: Image.asset(
                    width: 28.5, height: 28.5, "images/ic_message_true.png"),
                label: '',
              ),
              BottomNavigationBarItem(
                icon: Image.asset(
                    width: 28.5, height: 28.5, "images/ic_mine_false.png"),
                activeIcon: Image.asset(
                    width: 28.5, height: 28.5, "images/ic_mine_true.png"),
                label: '',
              ),
            ],
          ),
        ),
        onWillPop: () async {
          return false;
        });
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  bool get wantKeepAlive => true;
}
